import {Service, aspect, log} from "@codefree/badger/service";
import operationLog from '../middleware/operation_log'

/**
 * Service 内置方法，需要modelMapping到数据库实体
 * create
 * delete
 * update
 * query
 *
 - this.ctx
 - this.ctx.model
 - this.ctx.config
 - this.otherService
 */
@aspect.modelMapping('Dictionary')
@operationLog(/Query$/i)
export default class DictionaryService extends Service {
    async updateDescription(id: Number, description: String) {
        let values = {
            description: description
        };
        let option = {
            where: {
                id: id
            }
        };
        // update Dictionary set description = @description where id = @id
        await this.ctx.model.Dictionary.update(values, option)
    }

    async selfQuery(filter: Object) {
        const queryOption = {bind: {key: filter.key}};
        return this.pagination("SELECT * FROM dictionary where `key` = $key", queryOption, 1, 10);
    }
}
